package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.domain.entity.CustomerUser;
import com.ruoyi.system.domain.CustomerInfo;
import com.ruoyi.system.mapper.CustomerInfoMapper;
import com.ruoyi.system.mapper.CustomerMapper;
import com.ruoyi.system.mapper.CustomerUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.CartInfoMapper;
import com.ruoyi.system.domain.CartInfo;
import com.ruoyi.system.service.ICartInfoService;

/**
 * 购物车Service业务层处理
 *
 * @author ruoyi
 * @date 2025-04-11
 */
@Service
public class CartInfoServiceImpl implements ICartInfoService {
    @Autowired
    private CartInfoMapper cartInfoMapper;

    @Autowired
    private CustomerInfoMapper customerInfoMapper;

    @Autowired
    private CustomerMapper customerUserMapper;

    /**
     * 查询购物车
     *
     * @param id 购物车主键
     * @return 购物车
     */
    @Override
    public CartInfo selectCartInfoById(Long id) {
        return cartInfoMapper.selectCartInfoById(id);
    }

    /**
     * 查询购物车列表
     *
     * @param cartInfo 购物车
     * @return 购物车
     */
    @Override
    @Anonymous
    public List<CartInfo> selectCartInfoList(CartInfo cartInfo) {
        return cartInfoMapper.selectCartInfoList(cartInfo);
    }

    /**
     * 新增购物车
     *
     * @param cartInfo 购物车
     * @return 结果
     */
    @Override
    public List<CartInfo> insertCartInfo(CartInfo cartInfo) {
        CustomerInfo info = customerInfoMapper.selectUserByContactName(cartInfo.getCustomerName());
        CustomerUser user = customerUserMapper.selectUserByUserName(cartInfo.getUserName());
        cartInfo.setUserId(user.getUserId());
        cartInfo.setCustomerNumber(info.getContactNumber());
        cartInfoMapper.insertCartInfo(cartInfo);
        CartInfo newInfo = new CartInfo();
        newInfo.setUserId(user.getUserId());
        newInfo.setUserName(cartInfo.getUserName());
        List<CartInfo> list = cartInfoMapper.selectCartInfoByUser(newInfo);
        return list;
    }

    /**
     * 修改购物车
     *
     * @param cartInfo 购物车
     * @return 结果
     */
    @Override
    public int updateCartInfo(CartInfo cartInfo) {
        return cartInfoMapper.updateCartInfo(cartInfo);
    }

    /**
     * 批量删除购物车
     *
     * @param ids 需要删除的购物车主键
     * @return 结果
     */
    @Override
    public int deleteCartInfoByIds(Long[] ids) {
        return cartInfoMapper.deleteCartInfoByIds(ids);
    }

    @Override
    public int removeByUserName(String userName) {
        return cartInfoMapper.removeByUserName(userName);
    }

    /**
     * 删除购物车信息
     *
     * @param id 购物车主键
     * @return 结果
     */
    @Override
    public int deleteCartInfoById(Long id) {
        return cartInfoMapper.deleteCartInfoById(id);
    }
}
